package com.ohuang.demo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.Xfermode;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        Paint paint = new Paint();
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.MULTIPLY));
        // 定义渐变色
        int[] colors = {0x00FFFFFF, 0x00FFFFFF, 0xFFFFFFFF};
        float[] positions = {0, 0.9f, 1}; // 渐变位置

//        // 创建线性渐变
        LinearGradient linearGradient = new LinearGradient(
                0, 0, 0, height, // 渐变起点和终点
                colors, positions, Shader.TileMode.CLAMP // 颜色和位置
        );
        paint.setShader(linearGradient);
//        paint.setColor(0xFFFFFFFF);
        canvas.drawRect(new Rect(0,0,getWidth(),getHeight()),paint);

    }
}
